<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Repository\Product\GoodsNoticeRepository;
use App\Repository\Api\GoodsRepository as apiGoods;
use Illuminate\Support\Facades\DB;
use App\Api\Transfer;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class GoodsNoticeController extends BaseController
{
  public function __construct(GoodsNoticeRepository $GoodsNoticeRepository)
  {
    $this->model = $GoodsNoticeRepository;
  }
  public function getGoodsNoticeList(Request $request)
  {

    $shop_id = $request->get('shopId');
    $goods_id = $request->get('goods_id');
    $is_sup_notice = $request->get('is_sup_notice');
    $params = $request->all();
    $notice = $request->get('id');
    // var_dump($params, 111111111111);
    if (empty($shop_id)) {
      // return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }
    if (empty($goods_id) && empty($notice)) {
      return $this->returnMsg(100, '参数错误,请选择确定的商品信息');
    }
    if (!empty($is_sup_notice)) {
      if (!empty($params['province'])) {
        $arr = explode(' ', $params['province']);
        // var_dump($arr, 44444444444);
      }
      // 根据商品id 查询 编码
      $res = DB::table('goods_notice')->where(['id' => $notice])->first();
      $channel_code = '';
      if (!empty($res->remark)) {
        $channel_code = $res->remark;
      }
      // var_dump($arr, 555555);
      $arrays = [
        'city' => !empty($arr[1]) ? str_replace('"', '', $arr[1]) : '郑州市',
        'province' => !empty($arr[0]) ? str_replace('"', '', $arr[0]) : '河南省',
        'is_sup_notice' => $is_sup_notice,  //上游编码
        'channel_code' => $channel_code,
      ];
      // var_dump($arrays);
      $res = $this->getSupNotice($arrays);
      // var_dump($res, 111111111);
      $array = [
        'text' => '',
      ];
      if (!empty($res['data'])) {
        if (!empty($res['data']['protocolList']) && !empty($res['data']['protocolList'][0]) && !empty($res['data']['protocolList'][0]['content'])) {
          $array['text'] = $res['data']['protocolList'][0]['content'];
        }
      }
      return $this->returnMsg(200, '查询成功', $array);
    }

    // $shop_id = 1;
    // $where = [
    //   'where' => [
    //     'goods_notice.goods_id' => $goods_id
    //   ],
    //   'limit' => [
    //     'start' => 0,
    //     'number' => 20
    //   ],
    //   'order' => [
    //     'orderby' => 'sort',
    //     'orderdir' => 'desc'
    //   ],
    //   'select' => ['smilapp_notice.text', 'smilapp_notice.name', 'goods_notice.id', 'goods_notice.goods_name'],
    //   'left' => ['table' => 'smilapp_notice', 'left' => 'notice', 'right' => 'goods_notcice.id']
    // ];
    // $where = [
    //   'where' => [
    //     'goods_notice.goods_id' => $goods_id
    //   ],
    //   'limit' => [
    //     'start' => 0,
    //     'number' => 20
    //   ],
    //   'order' => [
    //     'orderby' => 'sort',
    //     'orderdir' => 'desc'
    //   ],
    //   'select' => ['smilapp_notice.text', 'smilapp_notice.name', 'goods_notice.id', 'goods_notice.goods_name'],
    //   'left' => ['table' => 'smilapp_notice', 'left' => 'notice', 'right' => 'goods_notcice.id']
    // ];
    // $allData = $this->model->index($where);
    $getAall  = DB::Table('smilapp_notice')->where(['id' => $notice])->first();
    // 将图片 处理成 绝对路径
    if (!empty($getAall)) {
      $getAall->text =  htmlspecialchars_decode($getAall->text);
    }
    return $this->returnMsg(200, '查询成功', $getAall);
  }
  // 获取渠道上游的告示
  public function getSupNotice($array)
  {
    $wayData = [];
    if (!empty($array['is_sup_notice'])) {
      $wayData = DB::Table('way')->where(['id' => $array['is_sup_notice']])->first();
      if (!$wayData) {
        return $this->returnMsg(102, '未查询到通道');
      }
      $wayData = json_decode(json_encode($wayData), true);
      $wayData['model'] = $array['is_sup_notice'];
    }
    $array = array_merge($array, $wayData);
    $model = new Transfer();
    $res = $model->getAgreement($array);
    return $res;
  }
}
